#include "MainView.h"
#include "Scroller.h"

#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QTimer>

#include <OLApplication.h>
#include <OLButton.h>
#include <OLPixmapWidget.h>

MainView::MainView(QWidget *parent)
    : QWidget(parent), m_count(0)
{
    // setup toolbar
    OLButton* btn = 0;
    btn = addToolbarButton(":/assets/icn_addmenu.png", "Show Dialog");
    connect(btn, SIGNAL(clicked()), this, SLOT(addMenu()));

    btn = addToolbarButton(":/assets/icn_addview.png", "Add View");
    connect(btn, SIGNAL(clicked()), this, SLOT(addBaseView()));

    btn = addToolbarButton(":/assets/icn_addstackview.png", "Add Stack View");
    connect(btn, SIGNAL(clicked()), this, SLOT(addView()));

    btn = addToolbarButton(":/assets/icn_removeview.png", "Toogle Highlight");
    connect(btn, SIGNAL(clicked()), btn, SLOT(toggleHighlight()));
    connect(btn, SIGNAL(clicked()), this, SLOT(showTextDialog()));

    // setup menu
    OLApp->addMenuItem("Hide Toolbar", qApp, SLOT(hideToolbar()));
    OLApp->addMenuItem("Show Toolbar", qApp, SLOT(showToolbar()));
    OLApp->addMenuItem("Portrait View", this, SLOT(addPortraitView()));
    OLApp->addMenuItem("Landscape View", this, SLOT(addLandscapeView()));
    OLApp->addMenuItem("Top Toolbar", qApp, SLOT(topToolbarPosition()));
    OLApp->addMenuItem("Bottom Toolbar", qApp,SLOT(bottomToolbarPosition()));
    OLApp->addMenuItem("Mini Toolbar", qApp,SLOT(miniToolbar()));
    OLApp->addMenuItem("Full Toolbar", qApp,SLOT(fullToolbar()));

    // set up button to load the kinetic view
    OLButton* kineticViewBtn = new OLButton(this);
    kineticViewBtn->textButton("Kinetic View");
    kineticViewBtn->opaque();
    kineticViewBtn->setGeometry(20,40,200,60);
    connect(kineticViewBtn, SIGNAL(clicked()), SLOT(addKineticView()));
}

void MainView::addKineticView()
{
    Scroller* kineticView = new Scroller(OLApp->viewParent());
    OLApp->addStackedView(kineticView, OLApplication::systemdefault, false);
}

void MainView::showTextDialog()
{
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    app->showBanner("This is a text dialog.", false);
}

OLButton* MainView::addToolbarButton(const QString& icon, const QString& text)
{
    OLWidgetList wgtList;
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    OLButton* btn = new OLButton();
    QVBoxLayout* btnLayout = new QVBoxLayout(btn);
    btnLayout->setMargin(0);
    btnLayout->setSpacing(5);
    btnLayout->setAlignment(Qt::AlignCenter);
    OLPixmapWidget* btnIcon = new OLPixmapWidget(icon);
    QLabel* btnText = new QLabel(text);
    btnText->setStyleSheet("font-size: 10px;");
    btnLayout->addWidget(btnIcon, 0, Qt::AlignHCenter);
    btnLayout->addWidget(btnText, 0, Qt::AlignHCenter);
    wgtList.append(btnText);
    btn->setLayout(btnLayout);
    app->addToolbarButton(btn, wgtList);

    return btn;
}

void MainView::addMenu()
{
    QString menuText("Menu Item ");
    menuText += QString::number(++m_count);

    OLButtonList buttonList;
    OLButton* dialogBtn = new OLButton("Ok");
    buttonList.append(dialogBtn);

    dialogBtn = new OLButton("Cancel");
    buttonList.append(dialogBtn);

    QVBoxLayout* body = new QVBoxLayout();
    body->setMargin(10);
    body->setSpacing(5);
    body->addWidget(new OLButton("Option 1"));
    body->addWidget(new OLButton("Option 2"));
    //OLApp->showDialog(QPixmap(":/assets/icn_addmenu.png"), "Test Dialog", body, OLButtonList());

    OLApp->showDialog(QPixmap(":/assets/icn_addmenu.png"), "Test Dialog", "This is a test message. This should hopefully bee multi line if the message is long enough!", buttonList);

    //OLApp->showSpinner();
}


void MainView::addView()
{
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    QWidget* stackedView =  new QWidget();
    stackedView->resize(app->viewSize());
    stackedView->setStyleSheet("color: white; background-color: rgb(99,99,99); border: 1px solid black;");
    QString viewLabel("Stacked view: ");
    viewLabel+=QString::number(++m_count);
    QLabel* test = new QLabel(viewLabel, stackedView);
    test->setStyleSheet("background-color: none; border: none;");
    app->addStackedView(stackedView, OLApplication::systemdefault, false);
}

void MainView::addBaseView()
{
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    app->setView(this, OLApplication::systemdefault, true);
}

void MainView::startApp()
{
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    QSize viewSize = app->viewSize();
    app->setView(this, OLApplication::systemdefault, true);
}


void MainView::addPortraitView()
{
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    QWidget* stackedView =  new QWidget();
    stackedView->resize(app->viewSize());
    stackedView->setStyleSheet("color: white; background-color: rgb(99,99,99); border: 1px solid black;");
    QString viewLabel("Stacked Portrait view: ");
    viewLabel+=QString::number(++m_count);
    QLabel* test = new QLabel(viewLabel, stackedView);
    test->setStyleSheet("background-color: none; border: none;");
    app->addStackedView(stackedView, OLApplication::portrait, false);
}

void MainView::addLandscapeView()
{
    OLApplication* app = dynamic_cast<OLApplication*>(qApp);
    QWidget* stackedView =  new QWidget();
    stackedView->resize(app->viewSize());
    stackedView->setStyleSheet("color: white; background-color: rgb(99,99,99); border: 1px solid black;");
    QString viewLabel("Stacked Landscape view: ");
    viewLabel+=QString::number(++m_count);
    QLabel* test = new QLabel(viewLabel, stackedView);
    test->setStyleSheet("background-color: none; border: none;");
    app->addStackedView(stackedView, OLApplication::landscape, false);
}

